﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class bidItem : System.Web.UI.Page
{
    public Item item = new Item();
    int itemId = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        itemId = Convert.ToInt32(HttpUtility.UrlDecode(HttpContext.Current.Request["itemId"].ToString()));
        item = InterfaceManager.DataManager.GetItem(itemId);

        bidTextBox.Bid_Click += new EventHandler(bidTextBox_Bid_Click);
    }

    
    protected void PanelRight_Load(object sender, EventArgs e)
    {
        List<Bid> bids = item.bids;
        bids.Sort(new Comparison<Bid>((Bid b1, Bid b2)=>
            {
                return b1.price.CompareTo(b2.price);
            }));
        User usr;
        for (int i = bids.Count - 1; i >= 0; i--)
        {
            control_BidRecord br = Page.LoadControl("~/control/BidRecord.ascx") as control_BidRecord;
            usr = InterfaceManager.DataManager.GetUsrBasicInfo(bids[i].user.userId);
            br.RecordUser = usr.userName;
            br.RecordTime = bids[i].datetime.ToString();
            br.RecordPrice = bids[i].price.ToString();
            PlaceHolderRight.Controls.Add(br);
        }
    }
    protected void PanelLeft_Load(object sender, EventArgs e)
    {
        if (item == null)
            return;
        ItemInformation.Image = "../images/" + item.picAddress; ;
        ItemInformation.ItemName = item.goodName;
        ItemInformation.ItemAuctioneer = item.auctioneer.userName;
        ItemInformation.ItemCondition = item.condition.ToString();
        ItemInformation.ItemCurrency = item.currency.ToString();
        ItemInformation.ItemCurrentPrice = item.currentPrice.ToString();
        ItemInformation.ItemDescription = item.description;
        ItemInformation.ItemPayMethod = item.payMethod.ToString();
    }
    public void btnReg_Click(object sender, EventArgs e)
    {
        if (TextBox2.Text.ToString().CompareTo(TextBox3.Text.ToString()) != 0)
        {
            return;
        }
        int userId = InterfaceManager.DataManager.Register(TextBox1.Text.ToString(), TextBox2.Text.ToString(), TextBox4.Text.ToString());
        if (userId == -1)
        {
            Response.Write("<script>alert('注册失败')</script>");
            return;
        }
        Session["usrId"] = userId;
    }
    public void btnLogin_Click(object sender, EventArgs e)
    {
        int userId = InterfaceManager.DataManager.Login(TxtUserName.Text.ToString(), TxtUserPwd.Text.ToString());
        if (userId == -1)
        {
            Response.Write("<script>alert('登录信息错误')</script>");
            return;
        }
        Session["usrId"] = userId;
    }
    protected void inputB_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/index.aspx?goodName=" + inputT.Text.ToString());
    }
    void bidTextBox_Bid_Click(object sender, EventArgs e)
    {
        int i = Convert.ToInt32(Session["usrId"]);
        if (i == 0)
        {
            Response.Write("<script>alert('您还未登录系统？')</script>");
           
            return;
        }
        if (item.currentPrice >= bidTextBox.BidPrice || item.reservePrice >= bidTextBox.BidPrice)
        {
            Response.Write("<script>alert('请输入大于当前price的价格')</script>");

            return;
        }
        InterfaceManager.DataManager.AddBid(i, itemId, bidTextBox.BidPrice, DateTime.Now);//?????????????????????
        Response.Redirect("~/bidItem.aspx?" + "itemId=" + itemId.ToString());
    }
}